package com.zhuanzhuan.hero.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuanzhuan.hero.user.consts.NumConsts;
import com.zhuanzhuan.hero.user.entity.HeroUserNotes;
import com.zhuanzhuan.hero.user.enums.ExceptionMsgEnum;
import com.zhuanzhuan.hero.user.enums.StateEnum;
import com.zhuanzhuan.hero.user.exception.BusinessException;
import com.zhuanzhuan.hero.user.mapper.HeroUserNotesMapper;
import com.zhuanzhuan.hero.user.service.HeroUserNotesService;
import com.zhuanzhuan.hero.user.util.Assert;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * dal Interface:HeroUserNotes
 * @author hanjunjun
 * @date 2024-6-25
 */
@Service
@Slf4j
public class HeroUserNotesServiceImpl extends ServiceImpl<HeroUserNotesMapper, HeroUserNotes>
    implements HeroUserNotesService {

    @Override
    public String queryNotesByUid(Long fromUid, Long toUid) {
        List<HeroUserNotes> heroUserNotes = this.listByUid(fromUid, toUid);
        if (CollectionUtils.isEmpty(heroUserNotes)) {
            log.info("不存在备注记录");
            return "";
        }
        return heroUserNotes.get(NumConsts.ZERO).getNotes();
    }

    @Override
    public List<HeroUserNotes> listByUid(Long fromUid, Long toUid) {
        log.info("listUserNotesByUid fromUid={}, toUid={}", fromUid, toUid);
        if (Objects.isNull(fromUid) || Objects.isNull(toUid)) {
            throw new BusinessException(ExceptionMsgEnum.PARAM_ERROR);
        }
        HeroUserNotes query = new HeroUserNotes();
        query.setFromUid(fromUid);
        query.setToUid(toUid);
        query.setState(StateEnum.EFFECTIVE.getCode());
        return this.getBaseMapper().select(query);
    }

    @Override
    public List<HeroUserNotes> listByUid(Long fromUid, List<Long> toUids) {
        if (Objects.isNull(fromUid) || CollectionUtils.isEmpty(toUids)) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<HeroUserNotes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HeroUserNotes::getFromUid, fromUid)
            .in(HeroUserNotes::getToUid, toUids)
            .eq(HeroUserNotes::getState, StateEnum.EFFECTIVE.getCode());

        return this.list(queryWrapper);
    }

    @Override
    public HeroUserNotes selectOneByUid(Long fromUid, Long toUid) {
        LambdaQueryWrapper<HeroUserNotes> query = new LambdaQueryWrapper<>();
        query.eq(HeroUserNotes::getFromUid, fromUid)
            .eq(HeroUserNotes::getToUid, toUid)
            .eq(HeroUserNotes::getState, StateEnum.EFFECTIVE.getCode());
        return this.getOne(query);
    }

    @Override
    public void setUpNotes(Long fromUid, Long toUid, String notes) {
        Assert.notNull(fromUid);
        Assert.notNull(toUid);
        if (fromUid.equals(toUid)) {
            log.info("uid相同，不保存备注");
            return;
        }
        if (Objects.isNull(notes)) {
            log.info("note为null 不更新");
            return;
        }

        HeroUserNotes userNotes = this.selectOneByUid(fromUid, toUid);
        if (Objects.nonNull(userNotes)) {
            //更新
            HeroUserNotes update = new HeroUserNotes();
            update.setId(userNotes.getId());
            update.setNotes(notes);
            this.updateById(update);
        } else {
            this.save(this.buildRecord(fromUid, toUid, notes));
        }
    }

    private HeroUserNotes buildRecord(Long fromUid, Long toUid, String notes) {
        HeroUserNotes heroUserNotes = new HeroUserNotes();
        heroUserNotes.setFromUid(fromUid);
        heroUserNotes.setToUid(toUid);
        heroUserNotes.setNotes(notes);
        heroUserNotes.setState(StateEnum.EFFECTIVE.getCode());
        heroUserNotes.setCreateBy(fromUid);
        heroUserNotes.setUpdateBy(fromUid);
        return heroUserNotes;
    }

}